package hnair.com.shop_ec.launcher;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;

import java.util.Timer;

import butterknife.BindView;
import butterknife.OnClick;
import hnair.com.shop.delegates.ShopDelegate;
import hnair.com.shop.util.timer.BaseTimerTask;
import hnair.com.shop.util.timer.ITimerListener;
import hnair.com.shop_ec.R;
import hnair.com.shop_ec.R2;

/**
 * Created by liaowei on 2017/11/17.
 */

public class LauncherDelegate extends ShopDelegate implements ITimerListener {

    @BindView(R2.id.tv_launcher_timer)
    AppCompatTextView mTvTimer = null;

    private Timer mTimer;
    private int mCount = 3;

    @OnClick(R2.id.tv_launcher_timer)
    void onClickTimerView() {

    }

    private void initTimer() {
        mTimer = new Timer();
        final BaseTimerTask task = new BaseTimerTask(this);
        mTimer.schedule(task, 0, 1000);
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_launcher;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        initTimer();
    }

    @Override
    public void onTimer() {
        getProxyActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mCount--;
                if (mCount < 0) {
                    if (mTimer != null) {
                        mTimer.cancel();
                        mTimer = null;
                    }
                }
            }
        });
    }
}
